package com.linhngua.code.gwt.client.ui;

import java.util.Collection;
import java.util.Iterator;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Widget;
import com.linhngua.code.gwt.client.data.shop.Item;

public class ItemView extends Composite {

	private static ItemViewUiBinder uiBinder = GWT
			.create(ItemViewUiBinder.class);

	interface ItemViewUiBinder extends UiBinder<Widget, ItemView> {
	}

	private Item item;

	@UiField
	Label itemName;
	@UiField
	FlexTable imageTable;
	@UiField
	Label itemPrice;
	@UiField
	Label itemDiscount;
	@UiField
	Label itemViewCount;
	@UiField
	Button itemLike;
	@UiField
	Label addedDate;

	@UiField
	TextArea itemDescription;

	public ItemView(Item item) {
		initWidget(uiBinder.createAndBindUi(this));
		this.item = item;
		itemName.setText(item.getName());
		populateImageTable();
		itemPrice.setText("Price: "
				+ NumberFormat.getCurrencyFormat(item.getCurrency())
						.format(item.getPrice()).toUpperCase());
		if (item.isOnSale()) {
			itemDiscount.setText("Discount: "
					+ NumberFormat.getPercentFormat()
							.format(item.getDiscount()));
		}
		itemViewCount.setText("View: " + item.getViewCount());
		itemLike.setText(NumberFormat.getFormat("#").format(item.getLikeCount()));
		addedDate.setText("Date added: "
				+ DateTimeFormat.getFormat(PredefinedFormat.DATE_SHORT).format(
						item.getAddedDate()));
		itemDescription.setText(item.getDescription());
	}

	@UiHandler("itemLike")
	void likeClick(ClickEvent event) {
		item.setLikeCount(item.getLikeCount() + 1);
		itemLike.setText(NumberFormat.getFormat("#").format(item.getLikeCount()));
	}

	/*
	 * This table only store 3 images
	 */
	void populateImageTable() {
		Collection<String> imageUrls = item.getImageUrls();
		if (imageUrls.size() > 0) {
			Iterator<String> urls = imageUrls.iterator();
			FlexCellFormatter cellFormatter = imageTable.getFlexCellFormatter();
			cellFormatter.setHorizontalAlignment(0, 0,
					HasHorizontalAlignment.ALIGN_CENTER);
			final Image bigImage = new Image(urls.next());
			bigImage.setStyleName("bigImage");
			imageTable.setWidget(0, 0, bigImage);
			cellFormatter.setColSpan(0, 0, 3);
			// cellFormatter.setRowSpan(0, 0, 3);
			int row = 1;
			int col = 0;
			for (String url : imageUrls) {
				final Image img = new Image(url);

				img.setStyleName("smallImage");
				imageTable.setWidget(row, col++, img);
				img.addClickHandler(new ClickHandler() {

					@Override
					public void onClick(ClickEvent arg0) {
						// show as main image
						Image next=new Image(img.getUrl());
						next.setStyleName("bigImage");
						imageTable.setWidget(0, 0, next);

					}
				});
			}

		}
	}

}
